/**
 * 初始化规则库定义详情对话框
 */
var SdsStandRuleCalDlg = {
    id: "SdsRuleCalTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
};

SdsStandRuleCalDlg.search = function () {
    SdsStandRuleCalDlg.refrsh();
};
SdsStandRuleCalDlg.refrsh = function () {
    var params = {};
    params['ruleId'] = $("#ruleId").val();
    SdsStandRuleCalDlg.table.refresh({query: params})
}

SdsStandRuleCalDlg.init = function (){
    var id = $("#id").val();
    var defaultColunms = SdsStandRuleCalDlg.initColumn();
    var table = new HISUITable(SdsStandRuleCalDlg.id, Sys.backendPath + "/sds/sdsstandrulebase/listCal/" + id, defaultColunms);
    table.setPaginationType("server");
    table.setSort("priority","asc");
    table.setSubHeight(-90);
    SdsStandRuleCalDlg.table = table.init();
}

/**
 * 检查是否选中
 */
SdsStandRuleCalDlg.check = function () {
    var selected = $('#' + SdsStandRuleCalDlg.id).datagrid('getSelections');
    if (selected.length == 0) {
        Sys.info("请先选中表格中的某一记录！");
        return false;
    } else {
        SdsStandRuleCalDlg.seItem = selected[0];
        return true;
    }
};

SdsStandRuleCalDlg.initColumn = function () {
    return [[
        {
            title: '当前时间',
            field: 'currentTime',
            align: 'center',
            width: 100,
            valign: 'middle',
            sortable: false,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "<span class='label label-success'>是</span>";
                } else {
                    return "<span class='label label-warning'>否</span>";
                }
            },
            editor: {
                type: 'combobox',
                options: {
                    required: true, valueField: 'id', textField: 'text', editable: false,
                    data:
                        [
                            {'id': 1, 'text': '是'},
                            {'id': 0, 'text': '否'}
                        ],
                },
            },
        },
        {title: '数据源', field: 'datasource', width: 200, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                for (let j = 0, len = dataMap.length; j < len; j++) {
                    if (value == dataMap[j].value) {
                        return dataMap[j].text;
                        break;
                    }
                }
            },
            editor: {
                type: 'combotree',
                options: {
                    url: '/sds/sdsstanddatasource/getDataSourceTree'
                }
            }
        },
        {
            title: '标化字典', field: 'dic', width: 200, align: 'center', valign: 'middle',id:'dic',
            formatter: function (value, row, index) {
                for (let j = 0, len = dicMap.length; j < len; j++) {
                    if (value == dicMap[j].value) {
                        return dicMap[j].text;
                        break;
                    }
                }
            },
            editor: {
                type: 'combotree',
                options: {
                    url: '/sds/sdsstanddic/getDicTree/' + "0"
                }
            }
        },
        {
            title: '取值时机', field: 'valueTime', width: 150, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                for (let j = 0, len = valueTimeMap.length; j < len; j++) {
                    if (value == valueTimeMap[j].id) {
                        return valueTimeMap[j].text;
                        break;
                    }
                }
            },
            editor: {
                type: 'combobox',
                options: {
                    valueField: 'id',
                    textField: 'text',
                    editable: false,
                    data: valueTimeMap
                }
            }
        },
        {
            title: '运算符', field: 'calculate', width: 100, align: 'center', valign: 'middle', editor: {
                type: 'text',
                options: {required: false}
            }
        },
        {
            title: '优先级', field: 'priority', width: 100, align: 'center', valign: 'middle',
            editor: {
                type: 'text',
                options: {required: true}
            }
        },
    ]];
};


/**
 * 关闭此对话框
 */
SdsStandRuleCalDlg.close = function () {
    parent.layer.close(parent.layer.getFrameIndex(window.name)); //先得到当前iframe层的索引,再执行关闭
}


/**
 * 添加新的一行
 */
SdsStandRuleCalDlg.addRuleCal = function () {
    var rowNum = $('#' + SdsStandRuleCalDlg.id).datagrid("getRows").length;
    //添加一行
    $('#' + SdsStandRuleCalDlg.id).datagrid('appendRow', {
        priority: rowNum + 1,
        datasource: '',
        dic: '',
        valueTime: '',
        calculate: '',
        currentTime: '0',
        ruleId: $("#id").val()
    });
    var rowIndex = rowNum;
    $('#' + SdsStandRuleCalDlg.id).datagrid('selectRow', rowIndex).datagrid('beginEdit', rowIndex);
}
/**
 * 保存运算
 */
SdsStandRuleCalDlg.saveRuleCal = function () {
    //保存完毕后，同步数据
    var operation_save = function () {
        SdsStandRuleCalDlg.table.acceptChanges();//调用datagrid自带的方法，确认其修改 然后保存至数据库
        var rows = $('#' + SdsStandRuleCalDlg.id).datagrid('getRows');
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            if (row != undefined && row != {}) {
                if (row.id != undefined) {
                    //update Row
                    Sys.ajax(Sys.backendPath + "/sds/sdsstandrulebase/updateCal", row, function (data) {
                        Sys.success("修改成功!");
                        SdsStandRuleCalDlg.refrsh();
                    }, function (data) {
                        Sys.error("修改失败!" + data.responseJSON.message + "!");
                    }, "json", false, "修改表达式保存至库中..");
                } else {
                    //add Row
                    Sys.ajax(Sys.backendPath + "/sds/sdsstandrulebase/addCal", row, function (data) {
                        Sys.success("修改成功!");
                        SdsStandRuleCalDlg.refrsh();
                    }, function (data) {
                        Sys.error("添加失败!" + data.responseJSON.message + "!");
                    }, "json", false, "新增表达式保存至库中..");
                }
            }
        }
    };
    Sys.confirm("是否保存表格数据至数据库?", operation_save);

};

/**
 * 删除规则
 */
SdsStandRuleCalDlg.delRuleCal = function () {
    if (SdsStandRuleCalDlg.check()) {
        var operation = function () {
            if (SdsStandRuleCalDlg.seItem.id != null && SdsStandRuleCalDlg.seItem.id != "") {
                var ajax = new $ax(Sys.backendPath + "/sds/sdsstandrulebase/delCal", function (data) {
                    Sys.success("删除成功!");
                    SdsStandRuleCalDlg.table.refresh();
                }, function (data) {
                    Sys.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("SdsRuleCalId", SdsStandRuleCalDlg.seItem.id);
                ajax.start();
            } else {
                SdsStandRuleCalDlg.table.delRow();
            }
        };
        Sys.confirm("是否删除该行数据?", operation);
    }
};

function Common_GetValue() {
    var itmValue = '';

    if (typeof (arguments[0]) !== 'string') return '';
    if (arguments[0] == '') return '';

    var $this = $('#' + arguments[0]);
    if ($this.length < 1) return '';

    var className = $this.attr("class").split(' ')[0];

    if (className == 'textbox') {  //文本框
        itmValue = $this.val();
    } else if (className == 'hisui-numberbox') {  //数字
        itmValue = $this.val();
    } else if (className == 'hisui-datebox') {  //日期
        itmValue = $this.datebox('getValue');
    } else if (className == 'hisui-timespinner') {  //时间框
        itmValue = $this.timespinner('getValue');
    } else if (className == 'hisui-combobox') {  //下拉框（多选下拉框没有封装）
        itmValue = $this.combobox('getValue');
    } else if (className == 'hisui-switchbox') {  //开关
        itmValue = $this.switchbox('getValue');
    } else if (className == 'hisui-checkbox') {  // 单个复选框
        itmValue = $this.checkbox('getValue');
    } else if (className == 'hisui-radio') {  //单个单选框
        itmValue = $this.radio('getValue');
    } else if (className == 'hisui-searchbox') {  //查询框框
        itmValue = $this.searchbox('getValue');
    }

    return itmValue;
}

$(function () {



});
